//
//  CommodityMobileCell.swift
//  iPhone
//
//  Created by Toj on 8/9/22.
//

import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class CommodityMobileCell: UICollectionViewCell {
    
    var productModel: MemberProductDto? { didSet {
        guard let product = productModel else { return }
        let defaultImage = "commodity_all_default".to_image
        let url = URL(string: product.background_image)
        backgroundImageView.kf.setImage(with: url, placeholder: defaultImage)
        
        nameLabel.text = product.name
        
        // TSH 20,000 / 30DAYS"
        var unitprice = ""
        if let commodity = product.commoditys.first {
            unitprice = commodity.currency_symbol + " " +
            commodity.price.st_moneyFormat.st_cleanZero
            
            let specs = commodity.specs_code
            let valid_time_num = commodity.valid_time_num
            let valid_time_en_name = commodity.valid_time_en_name
            // 非固定有效期
            if specs != .letValid &&
               valid_time_num > 0 && !valid_time_en_name.isEmpty {
                unitprice += " / " + commodity.cs_valid_time_num + String(valid_time_en_name)
            }
        }
        
        unitPriceLabel.text = unitprice
        setupMakeRelayoutSubviews()
        
        if !product.isLoad {
            product.isLoad = true
            DataStatistics.sendEvent(event: DSEvent(category: st_className, action: "ProdEntry", label: product.name, value: "1", lt: .event))
        }
    } }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
        backgroundImageView.st_cornerRadius = 8
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        setupMakeLayoutSubviews()
    }
    
    private lazy var backgroundImageView: UIImageView = {
        let view = UIImageView()
        view.contentMode = .scaleAspectFill
        return view
    }()
    
    private lazy var nameLabel: UILabel = {
        let view = UILabel()
        view.font = UIFont.systemFont(ofSize: 24.f_scale)
        view.textColor = .white
        return view
    }()
    
    private lazy var unitPriceLabel: UILabel = {
        let view = UILabel()
        view.textAlignment = .right
        view.font = UIFont.regular(with: 14.f_scale)
//        view.setContentHuggingPriority(.dragThatCanResizeScene, for: .horizontal)
        view.textColor = .white
        return view
    }()
}

// MARK: - 初始化
extension CommodityMobileCell {
    
    private func setupMakeAddSubviews() {
        
        contentView.addSubview(backgroundImageView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(unitPriceLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        
        backgroundImageView.snp.remakeConstraints { make in
            make.edges.equalTo(contentView)
        }
        
        setupMakeRelayoutSubviews()
    }
    
    // 实时修改布局否则更新数据会有问题, 未登录到登录刷新数据就会有问题
    private func setupMakeRelayoutSubviews() {
        
        unitPriceLabel.snp.remakeConstraints { make in
            make.right.equalTo(contentView.snp.right).offset(-10)
            make.centerY.equalTo(contentView)
        }
        
        nameLabel.snp.remakeConstraints { make in
            make.left.equalTo(16)
            make.centerY.equalTo(contentView)
            make.right.equalTo(unitPriceLabel.snp.left).offset(-1)
        }
    }
}
